package com.jsmy.marketing.activity;

import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.jsmy.marketing.R;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.base.BaseActivity;
import com.jsmy.marketing.bean.LoginBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.manager.UserManager;
import com.jsmy.marketing.utils.JPushUtils;
import com.jsmy.marketing.utils.MD5;
import com.jsmy.marketing.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.te_email)
    TextInputLayout teEmail;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.te_password)
    TextInputLayout tePassword;


    @Override
    protected int resId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onBaseCreate() {

    }

    @Override
    protected void initData() {
        etEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 11) {
                    //一定要在setError方法之后执行才可
                    teEmail.setError("11位手机号");
                    teEmail.setErrorEnabled(true);

                } else {
                    //不满足条件需要设置为false
                    teEmail.setErrorEnabled(false);
                }
            }
        });
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 15) {
                    //一定要在setError方法之后执行才可
                    tePassword.setError("请输入6-15位密码");
                    tePassword.setErrorEnabled(true);

                } else {
                    //不满足条件需要设置为false
                    tePassword.setErrorEnabled(false);
                }
            }
        });
        if (UserManager.getInstance().isLogin()) {
            etEmail.setText(UserManager.getInstance().getLid());
            etPassword.setText(UserManager.getInstance().getPassword());
            toLogin(etEmail.getText().toString().trim(), etPassword.getText().toString().trim());
        }
    }


    @OnClick({R.id.btn_login})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String email = etEmail.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                if (email.isEmpty() || email.length() != 11) {
                    ToastUtil.showShort(this, "账号格式错误，请重新输入");
                    return;
                }
                if (password.isEmpty() || password.length() > 15) {
                    ToastUtil.showShort(this, "密码格式错误，请重新输入");
                    return;
                }
                toLogin(email, password);
                break;
            default:
                break;
        }
    }

    /**
     * 登录
     *
     * @param email
     * @param password
     */
    private void toLogin(String email, String password) {
        if (TextUtils.isEmpty(email)) {
            ToastUtil.showShort(this, "手机号不能为空");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showShort(this, "密码不能为空");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("lid", email);
        map.put("pwd", MD5.md5(password));
        mPresenter.loadData(RetrofitManager.getInstance().login(map), Link.LOG_IN);
    }

    @Override
    public void refreshData(Object response, String position) {
        switch (position) {
            case Link.LOG_IN://登录
                LoginBean baseBean = (LoginBean) response;
                if (baseBean.getCode().equals("Y")) { //请求成功
                    finishDefault();
                    openActivity(MainActivity.class);
                    baseBean.getData().setPassword(etPassword.getText().toString().trim());
                    baseBean.getData().setLid(etEmail.getText().toString().trim());
                    UserManager.getInstance().saveUser(baseBean.getData());
                    UserManager.getInstance().setIsLogin(true);
                    JPushUtils.loginJMessage(this, UserManager.getInstance().getTagid()+"");
                } else {
                    ToastUtil.showShort(this, baseBean.getMsg());
                }
                break;
        }
    }
}
